import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/components/back_btn_icon.dart';
import 'package:share_fit/app/controllers/device/alarm/alarm_item.dart';
import 'package:share_fit/app/controllers/device/alarm/alarm_logic.dart';
import 'package:share_fit/app/controllers/device/alarm/alarm_state.dart';
import 'package:share_fit/app/controllers/device/alarm/label_dialog.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/color_const.dart';
import 'package:share_fit/generated/locales.g.dart';

class AlarmAddPage extends StatelessWidget {

  final logic = Get.put(AlarmLogic());
  int selectedValue=0;
  bool isEdit = false;
  int alarmIndex = 0;
  DateTime alarmTime = DateTime.now();

  @override
  Widget build(BuildContext context) {
    var argument = Get.arguments;
    if (argument != null && argument["isEdit"]) {
      isEdit = true;
      alarmIndex = argument["index"];
      AlarmItem alarm = logic.state.itemDatas[alarmIndex];
      alarmTime = DateTime(alarmTime.year, alarmTime.month, alarmTime.day, alarm.hour, alarm.minute);
      logic.state.alarmLabel = alarm.name;
    }
    else {
      logic.state.alarmVibrate = '';
      logic.state.alarmLabel = '';
      logic.state.alarmRepeat = '';
      logic.state.cycleListInt = 0;
      logic.state.cycleListIntIOS = 0;
      logic.state.alarmRepeat1 = false;
      logic.state.alarmRepeat2 = false;
      logic.state.alarmRepeat3 = false;
      logic.state.alarmRepeat4 = false;
      logic.state.alarmRepeat5 = false;
      logic.state.alarmRepeat6 = false;
      logic.state.alarmRepeat7 = false;
    }

    return Scaffold(
      // backgroundColor: ColorConst.mainBgColor,
      appBar: AppBar(
        // backgroundColor: ColorConst.mainBgColor,
        title: Text(LocaleKeys.alarm_add.ts, style: TextStyle(fontSize: 54.sp, color: ColorConst.btnFontColor),),
        centerTitle: true,
        leading: BackBtnIcon(onTap: () => Get.back()),
        actions: [
          GestureDetector(
            onTap: (){
              // logic.state.cycleList => 对应sdk的cycleList字段
              //selectTime.hour/selectTime.minute =>sdk接口小时/分钟

              int index = 1;

              if (!isEdit) {
                if(logic.state.itemDatas.isNotEmpty) {
                  logic.state.itemDatas.sort((a, b)=> a.index.compareTo(b.index));
                  index = logic.state.itemDatas.last.index++;
                }
              }

              AlarmItem item = AlarmItem();
              if (isEdit) {
                item = logic.state.itemDatas[alarmIndex];
              }
              else {
                item = AlarmItem();
                item.index = index;
              }

              item.enable = !logic.state.alarmAddLater;
              item.hour = logic.state.selectTime.hour;
              item.minute = logic.state.selectTime.minute;
              item.cycleList = Platform.isIOS?[logic.state.cycleListIntIOS]:[logic.state.cycleListInt];
              item.name = logic.state.alarmLabel;

              if (isEdit) {
                logic.updateItem();
              }
              else {
                logic.addItem(item);
              }

              print('Platform.isIOS : ${Platform.isIOS}/ ${item.cycleList}');

              logic.setAlarmList(back: (result){
                print(result);
              });

              Get.back();
            },
            child: Container(
              margin: EdgeInsets.only(right: 40.w),
              child: Text(LocaleKeys.person_save.ts, style: TextStyle(fontSize: 43.sp, color: ColorConst.btnFontColor),),
            ),)
        ],
      ),
      body: Column(
        children: [
          Container(
            margin: EdgeInsets.only(top: 141.h),
            height: 452.h,
            child: CupertinoDatePicker(
                mode: CupertinoDatePickerMode.time,
                initialDateTime: alarmTime,
                use24hFormat: logic.state.is24HourFormat,
                onDateTimeChanged: (date){
                  logic.state.selectTime = date;
                }),),
          SizedBox(height: 104.h),
          _buildItem(LocaleKeys.alarm_repeat.ts, AlarmState.alarmRepeatId, false,
              onTap: () async {
                var result = await Get.toNamed('/device/alarm/repeat', arguments: {AlarmState.alarmRepeatId:logic.state.alarmRepeat});
                logic.updateAlarmRightText(AlarmState.alarmRepeatId, result);
              }),
          // _buildItem(LocaleKeys.alarm_vibrate.ts, AlarmState.alarmVibrateId, false,
          //     onTap: (){
          //       showModalBottomSheet(context: context, builder: (_){
          //         return Container(
          //           width: 1.sw,
          //           height: 834.h,
          //           decoration: BoxDecoration(
          //               color: Colors.white,
          //               borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10))
          //           ),
          //           child: Column(
          //             children: [
          //               SizedBox(height: 62.h,),
          //               Text(LocaleKeys.alarm_label.ts, style: TextStyle(fontSize: 54.sp, color: ColorConst.btnFontColor),),
          //               SizedBox(height: 82.h,),
          //               Container(
          //                   height: 345.h,
          //                   child: CupertinoPicker(
          //                       diameterRatio: 2,
          //                       squeeze: 0.7,
          //                       onSelectedItemChanged: (value){
          //                         selectedValue = value;
          //                       },
          //                       itemExtent: 30,
          //                       selectionOverlay: Container(),
          //                       children: logic.state.alarm_vibrate_select.map((e) {
          //                         return Text('$e${LocaleKeys.times.ts}');
          //                       }).toList())
          //               ),
          //               SizedBox(height: 87.h,),
          //               Row(
          //                 mainAxisAlignment: MainAxisAlignment.spaceAround,
          //                 children: [
          //                   GestureDetector(
          //                     onTap: (){Navigator.pop(context);},
          //                     child: Container(
          //                       child: Text(LocaleKeys.cancel.ts, style: TextStyle(fontSize: 42.sp, color: ColorConst.btnFontColor),),
          //                     ),
          //                   ),
          //                   Container(
          //                     width: 1.w,
          //                     height: 60.h,
          //                     color: Colors.grey,),
          //                   GestureDetector(
          //                     onTap: (){
          //                       logic.updateAlarmRightText(AlarmState.alarmVibrateId, logic.state.alarm_vibrate_select[selectedValue]);
          //                       Navigator.pop(context);
          //                     },
          //                     child: Container(
          //                       child: Text(LocaleKeys.ok.ts, style: TextStyle(fontSize: 42.sp, color: ColorConst.btnFontColor),),
          //                     ),
          //                   )
          //                 ],)
          //             ],),);
          //       });
          //     }),
          _buildItem(LocaleKeys.alarm_label.ts, AlarmState.alarmLabelId, false,
              onTap: (){
                showDialog(context: context, builder: (_){
                  return LabelDialog(title: LocaleKeys.alarm_label.ts, callback: (res){
                    // print(logic.state.alarmLabel);
                    // logic.updateAlarmRightText(AlarmState.alarmLabelId, res);
                  },);
                });
                // showModalBottomSheet(context: context, builder: (_){
                //   return Container(
                //     width: 1.sw,
                //     height: 834.h,
                //     decoration: BoxDecoration(
                //         color: Colors.white,
                //         borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10))
                //     ),
                //     child: Column(
                //       children: [
                //         SizedBox(height: 62.h,),
                //         Text(LocaleKeys.alarm_label.ts, style: TextStyle(fontSize: 54.sp, color: ColorConst.btnFontColor),),
                //         SizedBox(height: 82.h,),
                //         GestureDetector(
                //           behavior: HitTestBehavior.opaque,
                //           onTap: (){
                //             logic.updateAlarmRightText(AlarmState.alarmLabelId, LocaleKeys.alarm.ts);
                //             Navigator.pop(context);
                //           },
                //           child: Row(
                //             mainAxisAlignment: MainAxisAlignment.spaceBetween,
                //             children: [
                //               Container(
                //                 padding: EdgeInsets.only(left: 106.w,),
                //                 child: Text(LocaleKeys.alarm.ts, style: TextStyle(fontSize: 54.sp, color: ColorConst.btnFontColor),),
                //               ),
                //               Container(
                //                 padding: EdgeInsets.only(right: 76.w,),
                //                 child: Radio(value: true,
                //                   activeColor: ColorConst.accentColor,
                //                   groupValue: true,
                //                   onChanged: (value) {},),
                //               )
                //             ],),),
                //       ],),);
                // });
              }),
          // _buildItem(LocaleKeys.alarm_later.ts, '', true),
        ],),
    );
  }

  Widget _buildItem(String title, String id, bool showSwitch, {onTap}) {
    Widget widget = GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: onTap,
      child: Container(
        height: 175.h,
        margin: EdgeInsets.fromLTRB(42.w, 0, 42.w, 40.h),
        padding: EdgeInsets.only(left: 60.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(10)
        ),
        child: Row(
          children: [
            Text(title, style: TextStyle(fontSize: 42.sp, color: ColorConst.btnFontColor),),
            Expanded(child: Container()),
            GetBuilder<AlarmLogic>(
                id: id,
                builder: (_){
              return Text(logic.getAlarmRightText(id), style: TextStyle(fontSize: 42.sp, color: ColorConst.btnFontColor),);
            }),
            SizedBox(width: 76.w,),
            Visibility(
              visible: showSwitch,
              replacement: Container(
                margin: EdgeInsets.only(right: 36.w),
                child: Image.asset("assets/images/space/icon_space_arrow.png", width: 8,height: 18,),
              ),
              child: Transform.scale(
                  scale: 0.7,
                  child: GetBuilder<AlarmLogic>(
                    id: 'update_alarm_later',
                    builder: (_) {
                      return CupertinoSwitch(
                          value: logic.state.alarmAddLater,
                          activeColor: ColorConst.accentColor,
                          onChanged: (value) {
                            logic.updateAlarmLater(value);
                          });
                    },)),),
          ],
        ),),
    );
    return widget;
  }
}